function findDept(id, source) {
  for (let i = 0; i < source.length; i++) {
    const dept = source[i]
    if (dept.id === id) {
      return dept
    }
    if (dept.children && dept.children.length) {
      const rs = findDept(id, dept.children)
      if (rs) {
        return rs
      }
    }
  }
}

let contentOptions = []

const formContainer = {
  type: 'form',
  rules: {
    bussType: [
      {
        required: true,
        message: '业务类型不能为空'
      }
    ],
    contents: [
      {
        required: true,
        message: 'tab权限内容不能为空'
      }
    ],
    deptId: [
      {
        required: true,
        message: '请选择所属部门'
      }
    ]
  },
  columnCount: 2,
  body: [
    {
      type: 'select',
      prop: 'bussType',
      label: '业务类型',
      placeholder: '请选择业务类型',
      fieldNames: { label: 'dictLabel', value: 'dictValue' },
      api: '/system/dict/data/type/t_buss_type'
    },
    {
      type: 'select',
      prop: 'contents',
      label: 'tab权限内容',
      placeholder: '请选择tab权限内容',
      multiple: true,
      collapseTags: true,
      fieldNames: { label: 'dictLabel', value: 'dictValue' },
      api: '/system/dict/data/type/business_menu_tab_type'
    },
    {
      type: 'treeSelect',
      prop: 'deptId',
      label: '所属部门',
      placeholder: '请选择所属部门',
      props: { label: 'label', value: 'id', children: 'children' },
      checkStrictly: true,
      fieldNames: { label: 'dictLabel', value: 'dictValue' },
      api: '/system/user/deptTree',
      event: {
        loaded: (options) => {
          contentOptions = options
        }
      }
    }
  ]
}

const columns = [
  {
    field: 'bussType',
    title: '业务类型',
    comType: 'lowCode',
    body: {
      type: 'mapping',
      value: '${bussType}',
      cache: true,
      api: {
        url: '/system/dict/data/type/t_buss_type',
        responseAdaptor: (response) => {
          const data = response.data
          const map = {}
          data.forEach((item) => {
            map[item.dictValue] = item.dictLabel
          })
          return {
            data: map
          }
        }
      }
    }
  },
  {
    field: 'deptName',
    title: '所属业务部门'
  },
  {
    field: 'contents',
    title: 'tab权限内容',
    comType: 'lowCode',
    showOverflow: true,
    body: {
      type: 'mapping',
      value: '${contents}',
      api: {
        url: '/system/dict/data/type/business_menu_tab_type',
        responseAdaptor: (response) => {
          const data = response.data
          const map = {}
          data.forEach((item) => {
            map[item.dictValue] = item.dictLabel
          })
          return {
            data: map
          }
        }
      }
    }
  },
  {
    field: 'operation',
    title: '操作',
    comType: 'lowCode',
    body: {
      type: 'space',
      body: [
        {
          type: 'link',
          actionType: 'dialog',
          reflectAttr: { type: 'primary' },
          body: '修改',
          dialog: {
            title: '修改',
            body: {
              ...formContainer,
              initApi: {
                url: '/menu/config/selectMenuConfigById/${id}',
                initDataFields: ['deptName']
              },
              api: {
                url: '/menu/config/updateMenuConfig',
                method: 'post',
                requestAdaptor: (config) => {
                  console.log(config)
                  const dept = findDept(Number(config.data.deptId), contentOptions)
                  config.data.deptName = dept.label
                  return config
                },
                data: {
                  bussType: '${bussType}',
                  contents: '${contents}',
                  deptId: '${deptId}',
                  id: '${id}'
                }
              }
            }
          }
        },
        {
          type: 'link',
          actionType: 'confirm',
          reflectAttr: { type: 'danger' },
          messageContent: '确定要删除吗？',
          body: '删除',
          api: '/menu/config/deleteMenuConfigById/${id}'
        }
      ]
    }
  }
]
export default {
  type: 'page',
  body: {
    type: 'crud',
    columns,
    api: {
      url: '/menu/config/selectMenuConfigList',
      responseData: ''
    },
    tableTop: {
      type: 'el-row',
      reflectAttr: {
        type: 'flex',
        justify: 'end',
        align: 'center'
      },
      body: {
        type: 'button',
        body: '新增',
        actionType: 'dialog',
        reflectAttr: { type: 'primary' },
        dialog: {
          title: '新增',
          body: {
            ...formContainer,
            api: {
              url: '/menu/config/insertMenuConfig',
              method: 'post',
              data: {
                bussType: '${bussType}',
                contents: '${contents}',
                deptId: '${deptId}'
              }
            }
          }
        }
      }
    }
  }
}
